namespace PickGold.Charting
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;

    internal class DoubleDateNanValueConverter : DoubleConverter
    {
        public static string ConvertDateTimeToString(double dtValue, ChartValueType axisValuesType, DateTimeIntervalType dtIntervalType)
        {
            string str = null;
            if (dtIntervalType == DateTimeIntervalType.Auto)
            {
                if (((axisValuesType == ChartValueType.DateTime) || (axisValuesType == ChartValueType.Time)) || ((axisValuesType == ChartValueType.Date) || (axisValuesType == ChartValueType.DateTimeOffset)))
                {
                    str = DateTime.FromOADate(dtValue).ToString("g", CultureInfo.CurrentCulture);
                }
            }
            else if (dtIntervalType != DateTimeIntervalType.Number)
            {
                if (dtIntervalType < DateTimeIntervalType.Hours)
                {
                    str = DateTime.FromOADate(dtValue).ToShortDateString();
                }
                else
                {
                    str = DateTime.FromOADate(dtValue).ToString("g", CultureInfo.CurrentCulture);
                }
            }
            if ((axisValuesType == ChartValueType.DateTimeOffset) && (str != null))
            {
                str = str + " +0";
            }
            return str;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            object obj2 = null;
            DateTime time;
            bool flag = false;
            string strA = value as string;
            if ((strA != null) && (string.Compare(strA, "NotSet", StringComparison.OrdinalIgnoreCase) == 0))
            {
                return (double) 1.0 / (double) 0.0;
            }
            if (((context != null) && (context.Instance != null)) && (context.Instance is Axis))
            {
                Axis axis = null;
                if (context.Instance is AxisScaleView)
                {
                    axis = ((AxisScaleView) context.Instance).axis;
                }
                if ((axis != null) && (strA != null))
                {
                    if (axis.InternalIntervalType == DateTimeIntervalType.Auto)
                    {
                        if (((axis.GetAxisValuesType() == ChartValueType.DateTime) || (axis.GetAxisValuesType() == ChartValueType.Date)) || ((axis.GetAxisValuesType() == ChartValueType.Time) || (axis.GetAxisValuesType() == ChartValueType.DateTimeOffset)))
                        {
                            flag = true;
                        }
                    }
                    else if (axis.InternalIntervalType != DateTimeIntervalType.Number)
                    {
                        flag = true;
                    }
                }
            }
            try
            {
                obj2 = base.ConvertFrom(context, culture, value);
            }
            catch (ArgumentException)
            {
                obj2 = null;
            }
            catch (NotSupportedException)
            {
                obj2 = null;
            }
            if (((strA != null) && (flag || (obj2 == null))) && DateTime.TryParse(strA, CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
            {
                return time.ToOADate();
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if ((destinationType == typeof(string)) && double.IsNaN((double) value))
            {
                return "NotSet";
            }
            if ((context != null) && (context.Instance != null))
            {
                Axis axis = null;
                if (context.Instance is AxisScaleView)
                {
                    axis = ((AxisScaleView) context.Instance).axis;
                }
                if ((axis != null) && (destinationType == typeof(string)))
                {
                    string str = ConvertDateTimeToString((double) value, axis.GetAxisValuesType(), axis.InternalIntervalType);
                    if (str != null)
                    {
                        return str;
                    }
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ArrayList values = new ArrayList();
            values.Add((double) 1.0 / (double) 0.0);
            return new TypeConverter.StandardValuesCollection(values);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

